package avicit.bdp.dms.tdm.rest;

import avicit.bdp.dms.tdm.dto.BdpAssetDTO;
import avicit.bdp.dms.tdm.service.BdpAssetService;
import avicit.platform6.core.exception.BusinessException;
import avicit.platform6.core.rest.msg.QueryRespBean;
import avicit.platform6.core.rest.msg.ResponseMsg;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import javax.annotation.Resource;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
@Api(tags = "数据资产API")
@RequestMapping("/api/bdp/dms/asset")
public class BdpAssetRest {

  @Resource private BdpAssetService bdpAssetService;

  @ApiOperation(value = "分页查询")
  @ApiImplicitParams({
    @ApiImplicitParam(value = "目录ID", name = "catalogId", dataType = "String", paramType = "query"),
    @ApiImplicitParam(value = "查询关键词", name = "keyWords", dataType = "String", paramType = "query"),
    @ApiImplicitParam(value = "状态", name = "status", dataType = "Int", paramType = "query"),
    @ApiImplicitParam(value = "资源类型", name = "assetType", dataType = "String", paramType = "query"),
    @ApiImplicitParam(
        value = "标题",
        name = "releaseTitle",
        dataType = "String",
        paramType = "query"),
    @ApiImplicitParam(
        value = "发布状态",
        name = "releaseStatus",
        dataType = "String",
        paramType = "query"),
    @ApiImplicitParam(
        value = "发布日期开始",
        name = "releaseDateBegin",
        dataType = "String",
        paramType = "query"),
    @ApiImplicitParam(
        value = "发布日期结束",
        name = "releaseDateEnd",
        dataType = "String",
        paramType = "query"),
    @ApiImplicitParam(value = "当前页", name = "pageNo", dataType = "Int", paramType = "query"),
    @ApiImplicitParam(value = "每页大小", name = "pageSize", dataType = "Int", paramType = "query")
  })
  @GetMapping
  public ResponseMsg<QueryRespBean<BdpAssetDTO>> getPageList(
      @RequestParam(required = false) String catalogId,
      @RequestParam(required = false) String keyWords,
      @RequestParam(required = false) String releaseTitle,
      @RequestParam(required = false) Integer releaseStatus,
      @RequestParam(required = false) String releaseDateBegin,
      @RequestParam(required = false) String releaseDateEnd,
      @RequestParam(required = false) Integer status,
      @RequestParam(required = false) String assetType,
      @RequestParam(defaultValue = "1") Integer pageNo,
      @RequestParam(defaultValue = "10") Integer pageSize)
      throws Exception {

    ResponseMsg<QueryRespBean<BdpAssetDTO>> responseMsg = new ResponseMsg<>();

    QueryRespBean<BdpAssetDTO> queryRespBean =
        bdpAssetService.getPageList(
            catalogId,
            assetType,
            keyWords,
            releaseTitle,
            releaseStatus,
            releaseDateBegin,
            releaseDateEnd,
            status,
            pageNo,
            pageSize);

    responseMsg.setResponseBody(queryRespBean);
    return responseMsg;
  }

  @GetMapping("/list")
  public ResponseMsg<List<BdpAssetDTO>> getList(
          @RequestParam(required = false) String catalogId,
          @RequestParam(required = false) String keyWords,
          @RequestParam(required = false) String releaseTitle,
          @RequestParam(required = false) Integer releaseStatus,
          @RequestParam(required = false) String releaseDateBegin,
          @RequestParam(required = false) String releaseDateEnd,
          @RequestParam(required = false) Integer status,
          @RequestParam(required = false) String assetType
  ) {

    try {
      ResponseMsg<List<BdpAssetDTO>> responseMsg = new ResponseMsg<>();
      List<BdpAssetDTO> result = bdpAssetService.getList(
              catalogId,
              assetType,
              keyWords,
              releaseTitle,
              releaseStatus,
              releaseDateBegin,
              releaseDateEnd,
              status);
      responseMsg.setResponseBody(result);
      return responseMsg;
    } catch (Exception e) {
      throw new RuntimeException(e);
    }
  }

  @ApiOperation(value = "新增")
  @PostMapping
  public ResponseMsg<BdpAssetDTO> save(@RequestBody BdpAssetDTO dto) {
    ResponseMsg<BdpAssetDTO> responseMsg = new ResponseMsg<>();
    String id = bdpAssetService.save(dto);
    if (StringUtils.isNotBlank(id)) {
      responseMsg.setResponseBody(bdpAssetService.queryById(dto.getId()));
    } else {
      throw new BusinessException("保存失败");
    }

    return responseMsg;
  }

  @ApiOperation(value = "修改")
  @PutMapping("/update")
  public ResponseMsg<BdpAssetDTO> update(@RequestBody BdpAssetDTO dto) {
    ResponseMsg<BdpAssetDTO> responseMsg = new ResponseMsg<>();
    bdpAssetService.update(dto);
    responseMsg.setResponseBody(bdpAssetService.queryById(dto.getId()));
    return responseMsg;
  }

  @ApiOperation(value = "通过ID查询")
  @ApiParam(value = "id", name = "id", required = true)
  @GetMapping("/{id}")
  public ResponseMsg<BdpAssetDTO> queryById(@PathVariable String id) {
    ResponseMsg<BdpAssetDTO> responseMsg = new ResponseMsg<>();
    responseMsg.setResponseBody(bdpAssetService.queryById(id));
    return responseMsg;
  }

  @ApiOperation(value = "通过ID删除")
  @ApiParam(value = "id", name = "id", required = true)
  @DeleteMapping("/{id}")
  public ResponseMsg<Boolean> deleteById(@PathVariable String id) {
    ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
    responseMsg.setResponseBody(bdpAssetService.deleteById(id));
    return responseMsg;
  }

  @ApiOperation(value = "资产撤回申请")
  @ApiParam(value = "id", name = "id", required = true)
  @PostMapping("/revokeAsset/{id}")
  public ResponseMsg<Boolean> revokeAssetById(@PathVariable String id) {
    ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
    boolean flag = bdpAssetService.revokeAssetById(id);
    responseMsg.setResponseBody(flag);
    return responseMsg;
  }

  @ApiOperation(value = "资产发布申请")
  @ApiParam(value = "id", name = "id", required = true)
  @PostMapping("/publishAsset/{id}")
  public ResponseMsg<Boolean> publishAssetById(@PathVariable String id) {
    ResponseMsg<Boolean> responseMsg = new ResponseMsg<>();
    boolean flag = bdpAssetService.publishAssetById(id);
    responseMsg.setResponseBody(flag);
    return responseMsg;
  }
}
